package net.opentsdb.plugin.impl;

import com.stumbleupon.async.Deferred;
import net.opentsdb.core.TSDB;
import net.opentsdb.core.WriteableDataPointFilterPlugin;
import net.opentsdb.stats.StatsCollector;
import net.opentsdb.utils.enums.TsuidChecks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 写入数据过滤器
 *
 */
public class WriteDataPointFilter extends WriteableDataPointFilterPlugin {

    private static final Logger LOG = LoggerFactory.getLogger(WriteDataPointFilter.class);

    private TSDB tsdb;

    @Override
    public void initialize(TSDB tsdb) {
        this.tsdb = tsdb;
    }

    @Override
    public Deferred<Object> shutdown() {
        LOG.info("shutdown filter , don't do anything");
        return Deferred.fromResult(null);
    }

    @Override
    public String version() {
        return "0.0.1";
    }

    @Override
    public void collectStats(StatsCollector collector) {
        // null
    }

    /**
     * 默认直接通过
     *
     * @param metric The metric name for the data point
     * @param timestamp The timestamp of the data
     * @param value The value encoded as either an integer or floating point value
     * @param tags The tags associated with the data point
     * @return
     */
    @Override
    public Deferred<Boolean> allowHistogramPoint(
            final String metric,
            final long timestamp,
            final byte[] value,
            final Map<String, String> tags) {
        return Deferred.fromResult(true);
    }

    @Override
    public Deferred<Boolean> allowDataPoint(
            final String metric,
            final long timestamp,
            final byte[] value,
            final Map<String, String> tags,
            final short flags) {
        // 状态检查移入metacache
        /*byte[] tsuid = tsdb.getTsuid(metric, timestamp, tags);
        TsuidChecks flag = tsdb.getMetaCache().incrementTsuid(tsuid);

        LOG.debug("tsuid "+tsuid+" is flag "+flag+" decide "+(flag.getStatus()?"":"not")+" accept");*/

        return Deferred.fromResult(true);
    }

    // open filter
    @Override
    public boolean filterDataPoints() {
        return true;
    }
}
